import Notify from '../../vant/notify/notify';
//引入工具类
const util = require('../../utils/util');
// 加载配置文件
const config = require('../../utils/config');

const app = getApp()

Page({
    data: {
        loading: false,
        title: '',
        content: '',
        location: {},
        imgUrls: [],
        imgIds: [],
        id: ''
    },
    bindTitle(e) {
        this.setData({
            title: e.detail
        })
    },
    bindContent(e) {
        this.setData({
            content: e.detail
        })
    },
    chooseImage() {
        var _this = this;
        wx.chooseImage({
            count: 3 - _this.data.imgUrls.length,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                wx.showLoading({
                    title: '上传中...'
                })
                var apiUrl = "law/uploadImg";
                // tempFilePath可以作为img标签的src属性显示图片
                const tempFilePaths = res.tempFilePaths
                tempFilePaths.forEach((item, index) => {
                    wx.uploadFile({
                        url: `${config.httpUrl}${apiUrl}`,
                        filePath: tempFilePaths[index],
                        name: +new Date() + '',
                        success(res) {
                            if (index === tempFilePaths.length - 1) {
                                wx.hideLoading()
                            }
                            const data = JSON.parse(res.data)
                            _this.setData({
                                imgUrls: _this.data.imgUrls.concat(tempFilePaths[index]),
                                imgIds: _this.data.imgIds.concat(data.uploadImgId)
                            })
                            console.log(_this.data.imgUrls, _this.data.imgIds)
                        }
                    })

                })

            }
        })
    },
    previewImage: function(e) {
        let index = e.currentTarget.dataset.index,
            pictures = this.data.imgUrls;
        wx.previewImage({
            current: pictures[index],
            urls: pictures
        })
    },
    delImage(e) {
        let _this = this;
        // wx.showLoading({
        //   title: '正在删除...'
        // })
        var _index = this.data.imgIds.indexOf(e.target.dataset.id)
        // app.ajax({
        //   url: `${config.httpUrl}law/deleteImg`,
        //   method: 'post',
        //   data: {
        //     id: e.target.dataset.id
        //   },
        //   callback: function(error, data) {
        //     wx.hideLoading()
        _this.data.imgUrls.splice(_index, 1);
        _this.data.imgIds.splice(_index, 1);
        _this.setData({
            imgUrls: _this.data.imgUrls,
            imgIds: _this.data.imgIds
        })
        //   }
        // })
    },
    chooseLocation() {
        let _this = this;
        wx.chooseLocation({
            success: function(res) {
                console.log(res)
                _this.setData({
                    location: res
                })
            }
        })
    },
    handleClick() {
        let _this = this;
        if (_this.data.title == '') {
            Notify({
                text: '请输入标题',
                duration: 1000,
                selector: '#custom-selector'
            });
            return;
        }
        if (_this.data.content == '') {
            Notify({
                text: '请输入内容',
                duration: 1000,
                selector: '#custom-selector'
            });
            return;
        }
        if (!_this.data.location.address) {
            Notify({
                text: '请选择位置',
                duration: 1000,
                selector: '#custom-selector'
            });
            return;
        }
        if (!_this.data.imgIds.length) {
            Notify({
                text: '请上传图片',
                duration: 1000,
                selector: '#custom-selector'
            });
            return;
        }
        this.setData({
            loading: true
        })
        wx.showLoading({
            title: '提交中...'
        })
        var params = {
            title: this.data.title,
            content: this.data.content,
            memo: this.data.location.address + this.data.location.name,
            longitude: this.data.location.longitude,
            latitude: this.data.location.latitude,
            imageIds: this.data.imgIds.join(",")
        }
        if (!!this.data.id) {
            params.id = this.data.id
        }
        app.ajax({
            url: `${config.httpUrl}law/save`,
            method: 'post',
            data: params,
            callback: function(error, data) {
                _this.setData({
                    loading: false
                })
                wx.hideLoading();

                if (+data.data.code === 0) {
                    let pages = getCurrentPages();
                    let prevPage = pages[pages.length - 2];
                    prevPage.setData({
                        reload: true
                    })
                    wx.navigateBack();
                } else {
                    Notify({
                        text: data.data.msg,
                        duration: 1000,
                        selector: '#custom-selector'
                    });
                }

            }
        })
    },
    onLoad: function(options) {
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        prevPage.setData({
            reload: false
        })

        let _this = this;
        if (!options.id) return;
        wx.showLoading({
            title: '加载中...'
        })
        _this.setData({
            id: options.id
        })
        app.ajax({
            url: `${config.httpUrl}law/findById`,
            method: 'get',
            data: {
                id: _this.data.id
            },
            callback: function(error, data) {
                var imgUrls = []
                var imgIds = []
                data.data.images.forEach((item) => {
                    imgUrls.push(item.img_url)
                    imgIds.push(item.id)
                })
                _this.setData({
                    loading: false,
                    title: data.data.title,
                    content: data.data.content,
                    imgUrls: imgUrls,
                    imgIds: imgIds,
                    location: {
                        longitude: data.data.longitude,
                        latitude: data.data.latitude,
                        address: data.data.memo,
                        name: ''
                    }
                })
                wx.hideLoading();
            }
        })
    },
    onShow() {
        let pages = getCurrentPages();
        let curPage = pages[pages.length - 1];
        app.checkBind(curPage.route);
    }
})